package com.fare.taluo.core.domain.analyse.report.download.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.analyse.report.download.ExcelInfo;
import com.fare.taluo.core.domain.analyse.report.download.ExcelInfoRepository;
import com.fare.taluo.util.StringUtils;

@Repository
public class ExcelInfoRepositoryImpl extends EntityRepositoryHibernate
		implements ExcelInfoRepository {

	@Override
	public List<ExcelInfo> findAll() {
		// TODO Auto-generated method stub
		String hql = "from ExcelInfo e where 1=1  order by e.uploadDate";
		List<Object> list = new ArrayList<Object>();
		return super.find(hql, list.toArray(), ExcelInfo.class);
	}

	@Override
	public void saveOrUpdate(ExcelInfo excelInfo) {
		super.save(excelInfo);

	}

	@Override
	public void delete(ExcelInfo excelInfo) {
		super.remove(excelInfo);

	}

	@Override
	public ExcelInfo findById(Long id) {
		
		return  super.get(ExcelInfo.class, id);
	}

	@Override
	public List<ExcelInfo> findByTime(String date) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from ExcelInfo e where 1=1 ");
		if(StringUtils.isNotEmpty(date)){
			sbf.append(" and e.dateType < ? ");
			list.add(date);
		}
		List<ExcelInfo> listExcel =super.find(sbf.toString(), list.toArray(), ExcelInfo.class);
		return listExcel;
	}

	@Override
	public List<ExcelInfo> findBycode(String code, String date) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from ExcelInfo e where 1=1 ");
		if(StringUtils.isNotEmpty(code)){
			sbf.append(" and e.code =?  ");
			list.add(code);
		}
		if(StringUtils.isNotEmpty(date)){
			String start =date +"00";
			String end =date +"23";
			sbf.append(" and e.dateType >= ? and e.dateType <= ? ");
			list.add(start);
			list.add(end);
		}
		List<ExcelInfo> listExcel =super.find(sbf.toString(), list.toArray(), ExcelInfo.class);
		return listExcel;
	}

	@Override
	public List<ExcelInfo> findBycodeAndTime(String code, String date) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from ExcelInfo e where 1=1 ");
		if(StringUtils.isNotEmpty(code)){
			sbf.append(" and e.code =?  ");
			list.add(code);
		}
		if(StringUtils.isNotEmpty(date)){
			sbf.append(" and e.dateType = ? ");
			list.add(date);
		}
		List<ExcelInfo> listExcel =super.find(sbf.toString(), list.toArray(), ExcelInfo.class);
		if(listExcel!=null && listExcel.size()>0){
			return listExcel;
		}
		return null;
	}



}
